﻿using System;
using InstanceController.ConfigurationDAL;
using InstanceController.RolesPerfMonDAL;
using WADConfigureWebRole;

namespace InstanceConfigurer
{
	public partial class Manual : System.Web.UI.Page
	{

		protected void Page_Load(object sender, EventArgs e)
		{
			Master.Page.Title = "Manual Operation - Instance Controller Administration";
			if (Page.IsPostBack) return;

			GetConfigurationSettings();
		}

		protected void ButtonUpdate_Click(object sender, EventArgs e)
		{
			SaveConfigurationSettings();
		}

		protected void ButtonSetIntervals_Click(object sender, EventArgs e)
		{
			SetIntervals();
		}

		/// <summary>
		/// Gets the configuration settings from Azure table storage.
		/// </summary>
		private void GetConfigurationSettings()
		{
			// If the read finds nothing it returns null.
			Global.ConfigurationDataModel = ConfigurationDataMethods.ReadRow(Global.ConfigurationDataModel.PartitionKey,
																			 Global.ConfigurationDataModel.RowKey);
			// If the Global.ConfigurationDataModel is null then create it.
			if (Global.ConfigurationDataModel == null)
			{
				Global.InitializeConfigurationDataModelObject();
				ClearTextBoxes();
				return; // retun now - no point trying to set values here.
			}

			LabelPartitionKey.Text = Global.ConfigurationDataModel.PartitionKey;
			LabelRowKey.Text = Global.ConfigurationDataModel.RowKey;
			LabelRoleName.Text = Global.ConfigurationDataModel.RoleName;
			TextBoxManualInstanceCount.Text = Global.ConfigurationDataModel.ManualInstances.ToString();
			var manualMode = Global.ConfigurationDataModel.ManualMode;
			if (!manualMode)
				RadioButtonMode1.Checked = true;	//auto mode
			else
				RadioButtonMode2.Checked = true;
		}

		private void ClearTextBoxes()
		{
			LabelPartitionKey.Text = String.Empty;
			LabelRowKey.Text = String.Empty;
			TextBoxManualInstanceCount.Text = String.Empty;
		}

		private void SetIntervals()
		{
			var counterSettingsDataModelRow = CounterSettingsDataMethods.Read(Global.ConfigurationDataModel.SubscriptionID,
																			   PlugIn.ProcessorFriendlyName);

			counterSettingsDataModelRow.FastAttack = Int32.Parse(TextBoxFastAttack.Text);
			counterSettingsDataModelRow.LongInterval = Int32.Parse(TextBoxLongInterval.Text);
			counterSettingsDataModelRow.ShortInterval = Int32.Parse(TextBoxShortInterval.Text);
			counterSettingsDataModelRow.SpinUpDelay = Int32.Parse(TextBoxSpinUpDelay.Text);
			CounterSettingsDataMethods.UpdateCounterSettings(counterSettingsDataModelRow, true);
		}

		/// <summary>
		/// Saves the configuration settings and performance counters to Azure table storage.
		/// Performance counters are defined in code in the Global.asax.cs file which is where
		/// Azure Diagnostics are configrued and initialized.
		/// </summary>
		private void SaveConfigurationSettings()
		{
			if (Global.ConfigurationDataModel == null)
			{
				LabelError.Text = "ERROR: Configuration data is missing.";
				return;
			}
			if (Int32.Parse(TextBoxManualInstanceCount.Text) <= 0 || String.IsNullOrEmpty(TextBoxManualInstanceCount.Text))
			{
				LabelError.Text = "ERROR: Configuration instance count is invalid (must be 1 or higher).";
				return;
			}

			Global.ConfigurationDataModel.ManualMode = RadioButtonMode1.Checked ? false : true; //false is auto
			Global.ConfigurationDataModel.ManualInstances = Int32.Parse(TextBoxManualInstanceCount.Text);
			if (ConfigurationDataMethods.UpdateConfiguration(Global.ConfigurationDataModel, true))
				LabelError.Text = "Update Started";
			else
				LabelError.Text = "Failed to save configuration.";

		}

	}
}